<?php

require_once( 'flexical/model/listing.php' );

require_once( 'flexical/service/dao.php' );
require_once( 'flexical/service/report-generator.php' );

class Flexical_Widget_Shortcode_Handler
{
	var $atts;
	var $content;
	var $code;
	var $dao;

	public function __construct( $atts, $content = null, $code = "" )
	{
		$this->atts = $atts;
		$this->content = $content;
		$this->code = $code;

		$this->dao = new Flexical_Service_DAO();
	}

	public function handle()
	{
		extract( shortcode_atts( array(
			'key' => null,
		), $this->atts ) );

		if ( ! is_null( $key ) )
		{
			foreach ( $this->dao->read_listings() as $listing )
			{
				if ( $listing->shortcode_key == $key )
				{
					require_once( 'flexical/service/report-generator.php' );
					$generator = new Flexical_Service_Report_Generator();
					$report = $generator->generate( $listing );
					return $report;
				}
			}
		}

		return 'FLEXICAL ERROR - No such key: ' . $key;
	}
}

?>
